package main

import (
	"errors"
	"fmt"
	"os"
)

func main() {
	test_try_catch()
}

func test_err_file() {
	_, err := os.Open("non_exist_file.txt")
	if err != nil {
		fmt.Println(err)
		panic(err)
	}
}
func test_throw_err() {
	hello := func(name string) error {
		if len(name) == 0 {
			return errors.New("name is empty")
		}
		fmt.Println("hello", name)
		return nil
	}
	if err := hello(""); err != nil {
		fmt.Println(err)
	}
}

// try...catch
func test_try_catch() {
	fmt.Println(get(5))
	fmt.Println("finished")
}
func get(index int) (ret int) {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Some error happened!", r)
			ret = -1
		}
	}()
	arr := [3]int{2, 3, 4}
	return arr[index]
}
